<template>
  <div class="category-list">
    <div class="category-header">
      <h1>{{ pageTitle }}</h1>
    </div>
    <!-- 筛选器 -->
<!--    <el-card class="filter-section">
      <el-form :inline="true" class="filter-form">
        <el-form-item label="价格区间">
          <el-select v-model="filters.priceRange" placeholder="选择价格区间">
            <el-option label="全部" value=""/>
            <el-option label="0-100元" value="0-100"/>
            <el-option label="100-300元" value="100-300"/>
            <el-option label="300-500元" value="300-500"/>
            <el-option label="500元以上" value="500+"/>
          </el-select>
        </el-form-item>
        <el-form-item label="排序">
          <el-select v-model="filters.sortBy" placeholder="排序方式">
            <el-option label="最新上架" value="newest"/>
            <el-option label="价格从低到高" value="price-asc"/>
            <el-option label="价格从高到低" value="price-desc"/>
            <el-option label="销量优先" value="sales"/>
          </el-select>
        </el-form-item>
      </el-form>
    </el-card>-->


    <!-- 商品列表 -->
    <div class="product-list">
      <div
          v-for="product in products"
          :key="product.id"
          class="product-item"
      >
        <div class="product-grid">
          <div class="product-image-container">
            <img :src="product.image" class="product-image" alt="Product Image">
          </div>
          <div class="product-details">
            <h3 class="product-title" >{{ product.title }}</h3>
            <p class="product-desc">{{ product.description }}</p>
            <div class="product-price">
              <span class="price">¥{{ product.price }}</span>
            </div>
            <div class="product-sales">
              <span class="sales">销量: {{ product.salesVolume }}</span>
            </div>
            <div class="product-actions">
              <el-button type="primary" size="small" @click="viewDetail(product.id)">
                查看详情
              </el-button>

            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 分页器 -->
    <div class="pagination-section">
      <el-pagination
          v-model="currentPage"
          :page-size="12"
          :total="total"
          layout="prev, pager, next"
          @current-change="handlePageChange"
      />
    </div>
  </div>
</template>

<script>
import {getApByTargetApi, getApByTypeApi, getByPriceRange} from "@/api/ProductApi";
import router from "@/router";
import { useRoute } from "vue-router";
import {computed, ref, watch} from "vue";

export default {
  name: 'CategoryListPage',
  setup() {
    const route = useRoute();
    const products = ref([]);
    const currentPage = ref(1);
    const pageSize = 12;
    const total = ref(0);

    // 页面标题映射
    const titleMap = {
      recipient: {
        friend: '送给朋友的礼物',
        family: '送给家人的礼物',
        classmate: '送给同学的礼物',
        colleague: '送给同事的礼物'
      },
      occasion: {
        'fathers-day': '父亲节礼物',
        'mothers-day': '母亲节礼物',
        'valentines-day': '情人节礼物'
      },
      type: {
        tech: '技术/极客礼物',
        home: '家居生活礼物',
        creative: '创意礼物',
        toys: '玩具/游戏',
        luxury: '奢华物品',
        funny: '奇葩搞怪礼物'
      },
      price: {
        '0-20': '20元以下礼物',
        '20-50': '20-50元礼物',
        '50-100': '50-100元礼物',
        '100+': '100元以上礼物'
      }
    };

    // 计算页面标题
    const pageTitle = computed(() => {
      const { filterType, value } = route.params;
      return titleMap[filterType]?.[value] || '商品列表';
    });

    // 筛选条
    const filters = ref({
      priceRange: '',
      sortBy: 'newest'
    });

    // 根据路由参数筛选分类
    const fetchData = async (filterType, value) => {
      if (filterType && value) {
        let request;
        switch (filterType) {
          case 'recipient':
            // 按收件人筛选
            if (value === 'family') {
              request = await getApByTargetApi(1, currentPage.value, pageSize)
            } else if (value === 'friend') {
              request = await getApByTargetApi(2, currentPage.value, pageSize)
            } else if (value === "colleague") {
              request = await getApByTargetApi(3, currentPage.value, pageSize)
            }
            break;
          case 'type':
            // 按类别筛选
            if (value === 'tech') {
              request = await getApByTypeApi(1, currentPage.value, pageSize);
            } else if (value === 'home') {
              request = await getApByTypeApi(2, currentPage.value, pageSize);
            } else if (value === 'creative') {
              request = await getApByTypeApi(3, currentPage.value, pageSize);
            } else if (value === 'toys') {
              request = await getApByTypeApi(4, currentPage.value, pageSize);
            } else if (value === 'luxury') {
              request = await getApByTypeApi(5, currentPage.value, pageSize);
            } else if (value === 'funny') {
              request = await getApByTypeApi(6, currentPage.value, pageSize);
            }
            break;
          case 'price': {
            // 按价格区间筛选
            if (value === '100+') {
              request = await getByPriceRange(currentPage.value, pageSize,100,1000000);
            } else {
              const [minPrice, maxPrice] = value.split('-');
              request = await getByPriceRange(currentPage.value, pageSize,minPrice,maxPrice);

            }
            break;
          }
        }

        if (request) {
          if (request.data.code === 1) {
            products.value = request.data.data.records;
            total.value = request.data.data.total;
          }
        }
      }
    };

    // 监听路由变化并重新加载数据
    watch(() => route.params, () => {
      const { filterType, value } = route.params;
      fetchData(filterType, value);
    }, { immediate: true });

    // 查看商品详情
    const viewDetail = (id) => {
      router.push(`/product/${id}`);
    };



    // 分页切换
    const handlePageChange = (page) => {
      currentPage.value = page;
      window.scrollTo({ top: 0, behavior: 'smooth' });
      const { filterType, value } = route.params;
      fetchData(filterType, value);
    };

    return {
      products,
      currentPage,
      total,
      pageTitle,
      filters,
      viewDetail,
      handlePageChange
    };
  }
};
</script>



<style scoped>


.product-list {
  display: grid;
  grid-template-columns: 1fr;
  gap: 20px;
  margin: auto 200px auto 200px;
}

.product-item {
  border: 1px solid #ddd;
  padding: 20px;
  border-radius: 8px;
  overflow: hidden;
}

.product-grid {
  display: grid;
  grid-template-columns: 1fr 2fr;
  gap: 20px;
  align-items: center;
}
.pagination-section {
  position: fixed;
  bottom: 20px; /* 距离底部的距离 */
  left: 50%;
  transform: translateX(-50%);
  text-align: center;
  margin-top: 40px;
  margin-bottom: 20px;
  z-index: 1000; /* 确保分页器在其他内容之上 */
}
.product-image-container {
  text-align: center;
}

.product-image {
  max-width: 100%;
  height: auto;
  width: 200px; /* 调整图片宽度 */
}

.product-details {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.product-title {
  margin: 0;
  font-size: 1.2em;
}

.product-desc {
  margin: 10px 0;
  font-size: 0.9em; /* 调整描述文字大小 */
}

.product-price {
  font-weight: bold;
  font-size: 1em; /* 调整价格文字大小 */
}

.product-sales {
  margin-top: auto;
  font-size: 0.9em; /* 调整销量文字大小 */
}

.product-actions {
  display: flex;
  justify-content: space-between;
}

.product-actions button {
  margin-top: 10px; /* 调整按钮间距 */
}


.category-header {
  margin: auto 200px auto 200px;
}

.category-header h1 {
  font-size: 24px;
  color: #333;
}

.filter-section {
  margin: auto 200px auto 200px;
}

.filter-form {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
}


.el-row {
  margin: -10px;
}

.el-col {
  padding: 10px;
}

.product-card {
  height: 100%;
  transition: transform 0.3s;
}

.product-card:hover {
  transform: translateY(-5px);
}


.product-info {
  padding: 14px;
}


.product-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}


.sales {
  color: #999;
  font-size: 12px;
}


.load-more {
  text-align: center;
  margin-top: 30px;
}

/* 添加分页器样式 */
.pagination-section {
  text-align: center;
  margin-top: 40px;
  margin-bottom: 20px;
}

/* 确保最后一行不会出现不对齐的情况 */
.el-col {
  margin-bottom: 20px;
}
</style>